package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IApl_admre_auditFeign;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.pageInfo.Apl_admre_auditPageInfo;
import com.power.sifa.approve.service.Apl_admre_auditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 行政复议相关审批表控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_admre_auditController", tags = {"行政复议相关审批表"})
public class Apl_admre_auditController implements IApl_admre_auditFeign {

    @Resource
    private Apl_admre_auditService apl_admre_auditService;


    @Override
    @ApiOperation("查询行政复议相关审批表分页列表")
    @SaCheckPermission("apl_admre_audit_list")
    public ResponseDTO list(Apl_admre_auditPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_admre_audit> page = this.apl_admre_auditService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_admre_audit apl_admre_audit : page) {
                apl_admre_audit.geneText();
                list.add(apl_admre_audit);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber() <= 1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看行政复议相关审批表")
    @SaCheckPermission("apl_admre_audit_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_admre_audit apl_admre_audit = apl_admre_auditService.getById(id);
        if (apl_admre_audit != null) {
            apl_admre_audit.geneText();
        }
        return ResponseDTO.succData(apl_admre_audit);
    }

    @Override
    @ApiOperation("删除行政复议相关审批表")
    @SaCheckPermission("apl_admre_audit_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_admre_auditService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改行政复议相关审批表")
    @SaCheckPermission("apl_admre_audit_save")
    public ResponseDTO save(Apl_admre_audit apl_admre_audit) {
        Assert.notNull(apl_admre_audit, "apl_admre_audit不能为空");
        apl_admre_auditService.saveOrUpdate(apl_admre_audit);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "批量删除行政复议相关审批表")
    @SaCheckPermission("apl_admre_audit_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_admre_auditService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value = "导出excel")
    @SaCheckPermission("apl_admre_audit_list")
    public void output(HttpServletResponse response, Apl_admre_auditPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "行政复议相关审批表";
        String[] heads = {"applyid+type+毫秒", "申请id", "审批表类型", "案号", "案由", "承办人意见", "承办人处理日期", "承办人id", "承办人", "科领导意见", "科领导处理日期", "科领导id", "科领导", "局领导意见", "局领导处理日期", "局领导id", "局领导", "收到日期", "受理日期", "期满日期", "申请人", "被申请人", "行政复议申请", "状态"};
        String[] fields = {"id", "applyid", "type", "code", "reason", "firstremark", "firstdate", "firstid", "firstname", "secondremark", "seconddate", "secondid", "secondname", "thirdremark", "thirddate", "thirdid", "thirdname", "receiptdate", "acceptdate", "quedate", "applyname", "targetname", "admrequest", "status"};
        Page<Apl_admre_audit> page = this.apl_admre_auditService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value = "导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "行政复议相关审批表";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid+type+毫秒", "申请id", "审批表类型", "案号", "案由", "承办人意见", "承办人处理日期", "承办人id", "承办人", "科领导意见", "科领导处理日期", "科领导id", "科领导", "局领导意见", "局领导处理日期", "局领导id", "局领导", "收到日期", "受理日期", "期满日期", "申请人", "被申请人", "行政复议申请", "状态"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

    @Override
    @ApiOperation(value = "导入excel")
    public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id", "applyid", "type", "code", "reason", "firstremark", "firstdate", "firstid", "firstname", "secondremark", "seconddate", "secondid", "secondname", "thirdremark", "thirddate", "thirdid", "thirdname", "receiptdate", "acceptdate", "quedate", "applyname", "targetname", "admrequest", "status"};
        List<Apl_admre_audit> list = (List<Apl_admre_audit>) ExcelUtil.Import(fields, file, Apl_admre_audit.class);
        if (!CollectionUtils.isEmpty(list)) {
            this.apl_admre_auditService.saveAll(list);
        }
        return ResponseDTO.succ();
    }


    /**
     * 更新是否市领导审批字段
     * @param id
     * @param iscitymanager
     * @return
     */
    @Override
    public ResponseDTO updatesIscitymanager(String id, Integer iscitymanager) {
        Apl_admre_audit admre_audit = this.apl_admre_auditService.getById(id);
        if (!ObjectUtils.isEmpty(admre_audit)) {
            admre_audit.setIscitymanager(iscitymanager);
            this.apl_admre_auditService.saveOrUpdate(admre_audit);
            return ResponseDTO.succMsg("更新成功");
        }
        return ResponseDTO.succ().setData(ResponseDTO.wrap(500, "更新失败"));
    }

    /**
     * 获取最新一条子流程记录
     * @param parentApplyid 父流程id
     * @param procType 流程变量
     * @return
     */
    @Override
    public ResponseDTO getLastChildAudit(String parentApplyid, Integer procType) {
        return ResponseDTO.succData(this.apl_admre_auditService.getLastChildAudit(parentApplyid, procType));
    }

    @Override
    public ResponseDTO findAllAuditList(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        return ResponseDTO.succData(this.apl_admre_auditService.listAdmreAuditByApplyid(applyid));
    }


}
